<template>
  <div class="box-all">
    <div class="content-box w-card">
      <div class="title-box">
        <span>收款账户管理</span>
        <el-button style="margin-left:auto;" @click="addAccount">新增收款账户</el-button>
      </div>
      <div class="table-box">
        <el-table :height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
          :cell-style="{fontSize:'14px',color:'#7b7b7d'}" :data="tableData" stripe>
          <el-table-column type="index" width="55" label="序号" align="center" />
          <el-table-column prop="showName" label="账户别名" align="center">
            <template v-slot="scope">
              {{ scope.row.showName || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="accountType" label="账户类型" align="center">
            <template v-slot="scope">
              {{ scope.row.accountType || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="accountNum" label="账号" align="center">
            <template v-slot="scope">
              {{ scope.row.accountNum || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="accountName" label="收款账户名" align="center">
            <template v-slot="scope">
              {{ scope.row.accountName || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="openAddress" label="开户行" align="center">
            <template v-slot="scope">
              {{ scope.row.openAddress || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="operations" label="操作" align="center" width="100">
            <template v-slot="scope">
              <el-link style="font-size:14px;" type="danger" @click="setHandle(scope.row)">删除</el-link>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
  <replace ref="replaceRef" @dialogClose="getTableData" />
  <confirm-pass ref="confirmPassRef" @verification="deleteHandle" />
</template>

<script>
import { onMounted, reactive, ref, toRefs } from 'vue'
import { getWindowWidthAndHeight, warningDeal } from '@/utils/utils'
import replace from './components/replace.vue'
import confirmPass from '@/components/confirmPass.vue'
import api from './api'
export default {
  components: {
    replace,
    confirmPass,
  },
  setup() {
    const replaceRef = ref(null)
    const confirmPassRef = ref(null)

    const state = reactive({
      deleteId: null,
      tableHeight: 0,
      tableData: [],
    })

    function getTableData() {
      api.accountList().then(res => {
        if (res.code == '200') {
          state.tableData = res.data.list
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function addAccount() {
      replaceRef.value.dialogShow()
    }

    function setHandle(data) {
      state.deleteId = data.id
      const title = '删除提示'
      const msg = '是否确定删除该收款账户？'
      confirmPassRef.value.dialogShow(title, msg)
    }

    function deleteHandle() {
      const obj = {
        id: state.deleteId
      }
      api.accountRemove(obj).then(res => {
        if (res.code == '200') {
          getTableData()
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 200
      getTableData()
    })

    return {
      ...toRefs(state),
      replaceRef,
      confirmPassRef,
      getTableData,
      addAccount,
      setHandle,
      deleteHandle,
    }
  }
}
</script>

<style lang="scss" scoped>
.box-all {
  width: 100%;
  height: 100%;
  overflow: hidden;
  .content-box {
    width: 100%;
    height: 100%;
    overflow-y: auto;
    box-sizing: border-box;
    padding: 20px;
  }
}
.table-box {
  margin-top: 20px;
}
.title-box {
  display: flex;
  align-items: center;
  > span {
    font-size: 16px;
    font-weight: bold;
  }
}
</style>